
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class Client implements Runnable {

    private OutputStream out;
    private InputStream in;
    private SocketConnection socket;
    private boolean isRunning = true;
    public Vector albumList = new Vector();
    public Vector lstImages = new Vector();
    private HomeScreen homeScreen;
    public Vector lstImageNames = null;

    public Client(HomeScreen homeScreen) throws IOException {
        this.homeScreen = homeScreen;
        socket = (SocketConnection) Connector.open("socket://58.187.34.247:1234");
//        socket = (SocketConnection) Connector.open("socket://192.168.1.30:1234");
        in = socket.openInputStream();
        out = socket.openOutputStream();
        new Thread(this).start();
    }

    private void Close() {
        try {
            socket.close();
            in.close();
            out.close();
            isRunning = false;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run() {
        while (isRunning) {
            try {
                int lenght = in.available();
                byte[] datas = new byte[lenght];
                int count = in.read(datas);
                while (count < lenght) {
                    count += in.read(datas, count, lenght - count);
                }
                String message = new String(datas);
                if (!message.equals("")) {
                    System.out.println(message);
                }
                if (message.startsWith(MessageType.Message)) {
                } else if (message.startsWith(MessageType.Login)) {
                    Common.UserId = Integer.parseInt(message.substring(message.indexOf(":") + 1));
                } else if (message.startsWith(MessageType.ListAlbum)) {
                    String str = message.substring(message.indexOf(":") + 1);
                    while (true) {
                        if (str.indexOf(":") == -1) {
                            albumList.addElement(str);
                            break;
                        }
                        albumList.addElement(str.substring(0, str.indexOf(":")));
                        str = str.substring(str.indexOf(":") + 1);
                    }
                } else if (message.startsWith(MessageType.Image)) {
                    if (lstImages == null) {
                        lstImages = new Vector();
                    }
                    Vector messages = Util.Split(message, ":");
                    int n = Integer.parseInt(messages.elementAt(1).toString());
                    byte[] images = new byte[n];
                    int c = in.read(images);
                    while (c < n) {
                        c += in.read(images, c, n - c);
                    }
                    lstImages.addElement(images);
                } else if (message.startsWith(MessageType.ViewImage)) {
                    if (lstImages == null) {
                        lstImages = new Vector();
                    }
                    Vector messages = Util.Split(message, ":");
                    int n = Integer.parseInt(messages.elementAt(1).toString());
                    byte[] images = new byte[n];
                    int c = in.read(images);
                    while (c < n) {
                        c += in.read(images, c, n - c);
                    }
                    lstImages.addElement(images);
                } else if (message.equals(MessageType.EndImage)) {
                    lstImageNames = null;
                } else if (message.startsWith(MessageType.ListImageName)) {
                    if (lstImageNames == null) {
                        lstImageNames = new Vector();
                    }
                    lstImageNames.addElement(message.substring(message.indexOf(":") + 1));
                }
            } catch (Exception e) {
                e.printStackTrace();
                homeScreen.DisplayException(e);
                Close();
            }
            System.gc();
        }
    }

    public void Login(String username, String password) {
        try {
            String message = MessageType.Login + ":" + username + ":" + password;
            out.write(message.getBytes());
        } catch (Exception e) {
            homeScreen.DisplayException(e);
            Close();
        }
    }

    public void Register(String username, String password) {
        try {
            String message = MessageType.Register + ":" + username + ":" + password;
            out.write(message.getBytes());
        } catch (Exception e) {
            homeScreen.DisplayException(e);
            Close();
        }
    }

    public void ViewListAlbum() {
        try {
            out.write(MessageType.ListAlbum.getBytes());
        } catch (Exception e) {
            homeScreen.DisplayException(e);
            Close();
        }
    }

    public void CreateAlbum(String albumName) {
        try {
            out.write((MessageType.CreateAlbum + ":" + albumName).getBytes());
        } catch (Exception e) {
            homeScreen.DisplayException(e);
            Close();
        }
    }

    public void ViewImage(int index) {
        try {
            String message = MessageType.ViewImage + ":" + index;
            out.write(message.getBytes());
        } catch (Exception e) {
            homeScreen.DisplayException(e);
            Close();
        }
    }

    public void ViewListImage(int index) {
        try {
            String message = MessageType.ViewListImage + ":" + index;
            Common.Client.lstImages = new Vector();
            out.write(message.getBytes());
        } catch (Exception e) {
            homeScreen.DisplayException(e);
            Close();
        }
    }

    public void SendImage(String string) {
        try {
            byte[] images = FileUtil.ReadFile(string);
            String formatImage = string.substring(string.lastIndexOf('.') + 1);
            out.write((MessageType.UploadImage + ":" + images.length + ":" + formatImage + ":" + Common.AlbumId).getBytes());
            out.write(images);
        } catch (Exception e) {
            homeScreen.DisplayException(e);
            Close();
        }
    }

    public void SendImage(byte[] images) {
        try {
            out.write((MessageType.UploadImage + ":" + images.length + ":png:" + Common.AlbumId).getBytes());
            out.write(images);
        } catch (Exception e) {
            homeScreen.DisplayException(e);
            Close();
        }
    }

    public void SendNewUpload() {
        try {
            out.write(MessageType.NewUpload.getBytes());
        } catch (Exception e) {
            homeScreen.DisplayException(e);
            Close();
        }
    }

    public void Exit() {
        try {
            out.write(MessageType.Exit.getBytes());
        } catch (Exception e) {
            homeScreen.DisplayException(e);
            Close();
        }
    }

    public void ViewListImageName(int index) {
        try {
            String message = MessageType.ListImageName + ":" + index;
            Common.Client.lstImageNames = new Vector();
            out.write(message.getBytes());
        } catch (Exception e) {
            homeScreen.DisplayException(e);
            Close();
        }
    }

    void SendImageNext() {
        try {
            out.write(MessageType.ViewImageNext.getBytes());
        } catch (Exception e) {
        }
    }

    void SendImagePre() {
        try {
            out.write(MessageType.ViewImagePre.getBytes());
        } catch (Exception e) {
        }
    }
}
